public class Demo3 {
    private static class MyRunnable implements Runnable {
        public volatile boolean flag = false;
        @Override
        public void run() {
            while (!flag) {
                System.out.println("线程还在运转~");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("线程即将结束！");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        thread.start();
        Thread.sleep(10*1000);
        myRunnable.flag = true;
    }
}
